/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.dto.InventoryUI;
import me.zhengjie.dto.sqlServer.Inventory;
import me.zhengjie.dto.sqlServer.Processcard;
import me.zhengjie.repository.PalletRepository;
import me.zhengjie.repository.ProcesscardRepository;
import me.zhengjie.repository.StorageRepository;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.InventoryRepository;
import me.zhengjie.service.InventoryService;
import me.zhengjie.service.dto.InventoryDto;
import me.zhengjie.service.dto.InventoryQueryCriteria;
import me.zhengjie.service.mapper.InventoryMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author zhangYY
* @date 2024-01-12
**/
@Service
@RequiredArgsConstructor
public class InventoryServiceImpl implements InventoryService {

    private final InventoryRepository inventoryRepository;
    private final InventoryMapper inventoryMapper;
    private final StorageRepository storageRepository;
    private final PalletRepository palletRepository;
    private final ProcesscardRepository processcardRepository;

    @Override
    public PageResult<InventoryUI> queryAll(InventoryQueryCriteria criteria, Pageable pageable){
        Page<Inventory> page = inventoryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(x ->{
            InventoryUI inventoryUI = new InventoryUI();
            inventoryUI.setId(x.getId());
            inventoryUI.setBatch(x.getBatch());
            inventoryUI.setWorkNum(x.getWorkNum());
            inventoryUI.setInventoryStatus(x.getInventoryStatus());
            inventoryUI.setInBoundFinishTime(x.getInBoundFinishTime());
            String storageCode = storageRepository.findCodeById(x.getStorageId());
            inventoryUI.setStorageCode(storageCode);
            String palletCode = palletRepository.findPalletCodeById(x.getPalletId());
            inventoryUI.setPalletCode(palletCode);
            Processcard processcard = processcardRepository.findProcesscardById(x.getProcesscardId());
            String processcardCode = processcard.getProcesscardCode();
            inventoryUI.setProcesscardCode(processcardCode);
            inventoryUI.setLayerName(processcard.getLayerName());
            return inventoryUI;
        }));
    }

    @Override
    public List<InventoryDto> queryAll(InventoryQueryCriteria criteria){
        return inventoryMapper.toDto(inventoryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public InventoryDto findById(Integer id) {
        Inventory inventory = inventoryRepository.findById(id).orElseGet(Inventory::new);
        ValidationUtil.isNull(inventory.getId(),"Inventory","id",id);
        return inventoryMapper.toDto(inventory);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Inventory resources) {
        inventoryRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Inventory resources) {
        Inventory inventory = inventoryRepository.findById(resources.getId()).orElseGet(Inventory::new);
        ValidationUtil.isNull( inventory.getId(),"Inventory","id",resources.getId());
        inventory.copy(resources);
        inventoryRepository.save(inventory);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            inventoryRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<InventoryDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (InventoryDto inventory : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("批次", inventory.getBatch());
            map.put("工程号", inventory.getWorkNum());
            map.put("库存状态", inventory.getInventoryStatus());
            map.put("入库完成时间", inventory.getInBoundFinishTime());
            map.put("储位", inventory.getStorageId());
            map.put("载盘", inventory.getPalletId());
            map.put("工艺卡", inventory.getProcesscardId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<Inventory> getByWorkNum(String workNum) {
        return inventoryRepository.findInventoriesByWorkNumAndInventoryStatus(workNum,1);
    }
}